package cn.hackcloud.mutithread.juc;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestLock {
	public static void main(String[] args) {
		Ticket2 ticket = new Ticket2();
		new Thread(ticket, "1号窗口").start();
		new Thread(ticket, "2号窗口").start();
		new Thread(ticket, "3号窗口").start();
	}
}

class Ticket implements Runnable {
	private int tick = 100;
	private Lock lock = new ReentrantLock();

	@Override
	public void run() {
		while (true) {
			lock.lock();
			if (tick > 0) {
				try {
					System.out.println(Thread.currentThread().getName() + "完成售票，余票为:" + --tick);
				} finally {
					lock.unlock();
				}
			}
		}
	}

}

class Ticket2 implements Runnable {
	AtomicInteger tick=new AtomicInteger(100);
	@Override
	public void run() {
		while (true) {
			if (tick.get() > 0) {
				try {
					System.out.println(Thread.currentThread().getName() + "完成售票，余票为:" + tick.decrementAndGet());
				} finally {
				}
			}
		}
	}
}